//
//  EalbumsFatherController.m
//  电子相册
//
//  Created by zxy on 2019/1/23.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "EalbumsFatherController.h"
//所有照片
#import "AllPhotoController.h"
//会议
#import "MeetingController.h"
//合影
#import "GroupPhotoController.h"
//茶歇
#import "TeaBreakController.h"
@interface EalbumsFatherController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    UIButton *_oldBtn;
    CGFloat _cellHeight;
}

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)UIScrollView *contentScrol;
@end

@implementation EalbumsFatherController

static NSString *const ReusableCellWithIdentifier = @"ReusableCellWithIdentifier";

-(UITableView *)tabView{
    if(!_tabView){
        _tabView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tabView.dataSource = self;
        _tabView.delegate = self;
        _tabView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
        [_tabView registerClass:[UITableViewCell class] forCellReuseIdentifier:ReusableCellWithIdentifier];
    }
    return _tabView;
}


-(UIScrollView *)contentScrol{
    if(!_contentScrol){
        _contentScrol = [[UIScrollView alloc]initWithFrame:CGRectZero];
        _contentScrol.showsVerticalScrollIndicator = NO;
        _contentScrol.pagingEnabled = YES;
        _contentScrol.scrollEnabled = NO;
        _contentScrol.delegate = self;
        _contentScrol.contentSize = CGSizeMake(4*screenWidth, 0);
    }
    return _contentScrol;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tabView];
    [self setupNavBar];
    [self addChildCtl];
    [self addChildView];
    //创建底部上传图片按钮
    [self createUpLoadImage];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableViewChangeCellHeihgt:) name:changeContainControllerCellHeightName object:nil];
}

-(void)refreshTableViewChangeCellHeihgt:(NSNotification *)no{
    CGFloat height = [no.object floatValue];
    _cellHeight = height;
    self.contentScrol.frame = CGRectMake(0, 0, screenWidth, height);
    [self.tabView reloadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden =  NO;
}

#pragma mark -添加子视图
-(void)addChildCtl{
    AllPhotoController *ctl1 = [[AllPhotoController alloc]init];
    [self addChildViewController:ctl1];
    
    MeetingController *ctl2 = [[MeetingController alloc]init];
    [self addChildViewController:ctl2];
    
    GroupPhotoController *ctl3 = [[GroupPhotoController alloc]init];
    [self addChildViewController:ctl3];
    
    TeaBreakController *ctl4 = [[TeaBreakController alloc]init];
    [self addChildViewController:ctl4];
}

-(void)addChildView{
    NSInteger index = self.contentScrol.contentOffset.x / self.contentScrol.xmg_width;
    UIViewController *ctl = self.childViewControllers[index];
    if(ctl.view.superview)return;
    ctl.view.frame = CGRectMake(index*self.contentScrol.xmg_width, 0, self.contentScrol.xmg_width, self.contentScrol.xmg_height);
    [self.contentScrol addSubview:ctl.view];
}

#pragma mark -设置导航栏
-(void)setupNavBar{
    self.navigationItem.title = @"电子相册";
    
    //设置头部
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 340)];
    self.tabView.tableHeaderView = bgView;
    
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 300)];
    header.backgroundColor = [UIColor cyanColor];
    [bgView addSubview:header];
    //设置工具条
    UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, header.xmg_bottom, screenWidth, 40)];
    toolBar.backgroundColor = [UIColor lightGrayColor];
    [bgView addSubview:toolBar];
    NSArray *titles = @[@"全部图片(202)",@"会议",@"合影",@"茶歇"];
    CGFloat btnX = 0;
    for (NSInteger i = 0; i < titles.count; i++) {
        NSString *title = titles[i];
        CGFloat titleW = [Helper widthOfString:title font:[UIFont systemFontOfSize:14] height:toolBar.xmg_height] + 16;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        if(i == 0){
            btn.selected = YES;
            _oldBtn = btn;
        }
        btn.frame = CGRectMake(btnX, 0, titleW, toolBar.xmg_height);
        btnX += titleW;
        [btn setTitle:title forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [toolBar addSubview:btn];
    }
}

-(void)btnClick:(UIButton *)sender{
    _oldBtn.selected = NO;
    sender.selected = YES;
    _oldBtn = sender;
    [self.contentScrol setContentOffset:CGPointMake(screenWidth * sender.tag, 0) animated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:refreshControllerDatasName object:@(sender.tag)];
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self addChildView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _cellHeight;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusableCellWithIdentifier forIndexPath:indexPath];
    [cell.contentView addSubview:self.contentScrol];
    return cell;
}

#pragma mark -创建上传图片按钮
-(void)createUpLoadImage{
    UIButton *upLoadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    upLoadBtn.backgroundColor = [UIColor redColor];
    [upLoadBtn setTitle:@"上传图片" forState:UIControlStateNormal];
    [upLoadBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [upLoadBtn addTarget:self action:@selector(upLoadBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:upLoadBtn];
    [upLoadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark -上传图片按钮点击事件
-(void)upLoadBtnClick{
    NSLog(@"上传图片按钮被点击了");
    NSInteger index = self.contentScrol.contentOffset.x / screenWidth;
    [[NSNotificationCenter defaultCenter] postNotificationName:upLoadBtnClickName object:@(index)];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}



@end
